package dao;

import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Hotel;
import model.Rorder;
import model.RorderPK;
import model.Route;
import model.Tourist;
import model.View;
import model.Vroute;
import model.VroutePK;
import util.EntityManagerUtil;

public class VrouteDao {

	public static Vroute newVroute(String rname, String vname) throws Exception {
		Route route = RouteDao.getRouteNoCache(rname);
		View view = ViewDao.getViewNoCache(vname);
		Vroute vroute = new Vroute();
		VroutePK pk = new VroutePK();
		pk.setRName(route.getRname());
		pk.setVName(view.getName());
		vroute.setId(pk);
		return vroute;
	}

	public static boolean getVrouteNoCacheForBool(Vroute vroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Vroute c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Vroute VrouteFind;
		try {
			VrouteFind = (Vroute) query.setParameter("id", vroute.getId()).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			// e.printStackTrace();
			em.close();
			return false;
		}
	}

	// 查找信息无缓存
	public static Vroute getVrouteNoCache(Vroute vroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Vroute c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		try {
			Vroute vrouteFind = (Vroute) query.setParameter("id", vroute.getId()).getSingleResult();
			em.close();
			return vrouteFind;
		} catch (Exception e) {
			em.close();
			// TODO Auto-generated catch block
			System.out.println("查询Vroute失败");
			return null;
		}

	}

	public static boolean delVroute(Vroute vroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Vroute vrouteFind = getVrouteNoCache(vroute);
			if (vrouteFind != null) {
				Vroute delVroute = em.find(Vroute.class, vrouteFind.getId());
				em.getTransaction().begin();
				Route route = RouteDao.getRouteNoCache(vroute.getId().getRName());
				View view = ViewDao.getViewNoCache(vroute.getId().getVName());
				route.setMoney(route.getMoney() - view.getPrice());
				em.merge(route);
				em.remove(delVroute);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static boolean addVroute(Vroute vroute) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			if (!getVrouteNoCacheForBool(vroute)) {
				em.getTransaction().begin();
				Route route = RouteDao.getRouteNoCache(vroute.getId().getRName());
				View view = ViewDao.getViewNoCache(vroute.getId().getVName());
				route.setMoney(route.getMoney() + view.getPrice());
				em.merge(route);
				em.merge(vroute);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
}
